<?php
add_theme_support( 'post-thumbnails', array( 'post' ) );
set_post_thumbnail_size( 50, 50, true );
add_theme_support( 'post-formats', array( 'aside' ) );
add_theme_support( 'nav-menus' );
if (function_exists('wp_nav_menu')) register_nav_menus(array('primary' => 'Primary Navigation'));
wp_deregister_script( 'comment-reply' );
add_theme_support( 'automatic-feed-links' );
register_sidebar(array(
	'before_widget' => '<div class="footer-sidebar"><div id="%1$s" class="widget %2$s">'."\n",
	'after_widget' => "</div></div>\n",
	'before_title' => '<h3 class="widget-title">',
	'after_title' => "</h3>\n",
	'name' => __('SideBar', 'iboxs' ),
	'id' => 'footer-sidebar'
));
add_custom_background();
if ( ! isset( $content_width ) ) $content_width = 670;
if ( !is_admin() ) {
	function my_init_method() {
    	wp_deregister_script( 'jquery' );
     	wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js', '', '1.6.4' );
  	}
	add_action('template_redirect', 'my_init_method');
	wp_enqueue_script( 'boxs', get_bloginfo('template_directory').'/all.js', array('jquery'), '1.0.3', true );
	wp_enqueue_script( 'comments-ajax', get_bloginfo('template_directory').'/comments-ajax.js', array('jquery'), '1.2.3', true );
}


/* comment_mail_notify v1.0 by willin kan.  */
function comment_mail_notify($comment_id) {
   $admin_email = get_bloginfo ('admin_email'); 
   $comment = get_comment($comment_id);
   $comment_author_email = trim($comment->comment_author_email);
   $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
   $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
   $spam_confirmed = $comment->comment_approved;
   if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
     /* 上面的判斷式,決定發出郵件的必要條件:
     ($parent_id != '') && ($spam_confirmed != 'spam'): 回覆的, 而且不是 spam 才可發, 必需!!
     ($to != $admin_email) : 不發給 admin.
     ($comment_author_email == $admin_email) : 只有 admin 的回覆才可發.
     可視個人需求修改以上條件.
     */
     $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
     $subject = '童鞋, 你在 [' . get_option("blogname") . '] 的留言有了回应';
     $message = '
     <div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">
       <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
       <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
        . nl2br(get_comment($parent_id)->comment_content) . '</p>
       <p>' . trim($comment->comment_author) . ' 给您的回应:<br />'
        . nl2br($comment->comment_content) . '<br /></p>
       <p>您可以点击 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回应完整内容</a></p>
       <p>欢迎再度光临 <a href="' . home_url() . '">' . get_option('blogname') . '</a></p>
       <p>(此邮件由系统自动发出，请勿回复。)</p>
     </div>';
     $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
     $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
     wp_mail( $to, $subject, $message, $headers );
   }
}
 add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------
function pagenavi( $p = 10 ) {
	if ( is_singular() ) return;
	global $wp_query, $paged;
	$max_page = $wp_query->max_num_pages;
	if ( $max_page == 1 ) return;
	echo '<div id="pagenavi">';
	if ( empty( $paged ) ) $paged = 1;
	if ( $paged > 1 ) p_link( $paged - 1, '上一页', '上一页' );
	if ( $paged > $p + 1 ) p_link( 1, '最前页' );
	if ( $paged > $p + 2 ) echo '<span class="page-numbers dots">...</span> ';
	for( $i = $paged - $p; $i <= $paged + $p; $i++ ) if ($i > 0 && $i <= $max_page )$i == $paged ? print "<span class='page-numbers current'>{$i}</span> " : p_link( $i );
	if ( $paged < $max_page - $p - 1 ) echo '<span class="page-numbers dots">...</span> ';
	if ( $paged < $max_page - $p ) p_link( $max_page, '最后页' );
	if ( $paged < $max_page ) p_link( $paged + 1,'下一页', '下一页' );
	echo '</div>';
}
function p_link( $i, $title = '', $linktype = '' ) {
	if ( $title == '' ) $title = "第 {$i} 页";
	if ( $linktype == '' ) { $linktext = $i; } else { $linktext = $linktype; }
	echo "<a class='page-numbers' href='", esc_html( get_pagenum_link( $i ) ), "' title='{$title}'>{$linktext}</a> ";
}

add_action('admin_menu', 'iboxs_default_options');
function iboxs_options_items(){
	$items = array (
		array(
			'id' => 'keywords',
			'name' => __('网站关键词'),
			'desc' => __('网站关键词（Meta Keywords），中间用半角逗号隔开。')
		),
		array(
			'id' => 'description',
			'name' => __('网站描述'),
			'desc' => __('网站描述（Meta Description），针对搜索引擎设置的网页描述。')
		),
		array(
			'id' => 'analytics',
			'name' => __('统计代码设置')
		)
	);
	return $items;
}
add_action( 'admin_init', 'iboxs_options_init' );
function iboxs_options_init(){
	register_setting( 'iboxs_options', 'iboxs_theme_options', 'iboxs_options_validate' );
}
function iboxs_default_options() {
	$options = get_option( 'iboxs_theme_options' );
	foreach ( iboxs_options_items() as $item ) {
		if ( ! isset( $options[$item['id']] ) ) {
			$options[$item['id']] = '';
		}
	}
	add_theme_page( __( '主题选项' ), __( '主题选项' ), 'edit_theme_options', 'theme_options', 'iboxs_settings' );
	update_option( 'iboxs_theme_options', $options );
}
function iboxs_settings(){?>
<div class="wrap">
    <?php screen_icon(); echo "<h2>" . sprintf( __( '%1$s Theme Options'), get_current_theme() ). "</h2>"; ?>
	<form method="post" action="options.php">
		<?php settings_fields( 'iboxs_options' ); ?>
		<?php $options = get_option( 'iboxs_theme_options' ); ?>
		<table class="form-table">
		<?php foreach (iboxs_options_items() as $item) { ?>
				<tr valign="top" style="margin:0 10px;">
					<th scope="row"><?php echo $item['name']; ?></th>
					<td>
                    	<?php if($item['id'] != 'analytics'){?>
							<input name="<?php echo 'iboxs_theme_options['.$item['id'].']'; ?>" type="text" value="<?php if ( $options[$item['id']] != "") { echo $options[$item['id']]; } else { echo ''; } ?>" size="80" />
						<br/>
							<label class="description" for="<?php echo 'iboxs_theme_options['.$item['id'].']'; ?>"><?php echo $item['desc']; ?></label>
                        <?php }else{?>
                        	<textarea id="<?php echo $item['id']; ?>" name="<?php echo 'iboxs_theme_options['.$item['id'].']'; ?>" type="textarea" rows="4" cols="56"><?php if ( $options[$item['id']] != "") { echo $options[$item['id']]; } ?></textarea>
                        <?php } ?>
					</td>
				</tr>
			<?php } ?>
			</table>
			<p class="submit">
				<input type="submit" class="button-primary" value="<?php _e( 'Save Options'); ?>" />
			</p>
		</form>
		
	</div>
<?php
}
function iboxs_options_validate($input){ return $input;}

if ( ! function_exists( 'mytheme_comment' ) ) :
function mytheme_comment($comment, $args, $depth) {
   	$GLOBALS['comment'] = $comment;
  	switch ( $comment->comment_type ) :
		case 'pingback' :
		case 'trackback' :
?>
	<li class="pingback" style="border-bottom:1px solid #EEEEEE;">
		<p><?php _e( 'Pingback:' ); ?> <?php comment_author_link(); ?></p>
<?php
		break;
	default :
?>    
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
	<div id="comment-<?php comment_ID(); ?>" class="comment-main cl">
    	<?php echo get_avatar($comment,$size='32',$default='' );?>
        <div class="comment-content cl">
        	<div class="comment-author vcard">
        		<cite class="fn"><?php comment_author_link(); ?></cite> • <?php printf( __( '%1$s - %2$s', '' ), get_comment_date(),  get_comment_time() ); ?> • <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))); ?>
        	</div>
			<?php if ($comment->comment_approved == '0') : ?>
				<em><?php _e('Your comment is awaiting moderation.') ?></em>
				<br />
			<?php endif; ?>
			<div class="alignleft"><?php comment_text(); ?></div>
        </div>
    </div>
<?php break;	endswitch;}endif;